﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace LGE
{
    public class SpriteSheet
    {
        //public int nFrames; // número total de frames de cada animação
        public int interval;

        public EngineObject gameObject;
        public int frameWidth;
        public int frameHeight;
        public int actualWidth = 0;
        public int actualHeight = 0;
        //public int actualFrame = 0;
        public double intervalAcc = 0;
        public bool completed = false;
        public bool isLoop = false;
        
        public SpriteSheet(EngineObject gameObject, int frameWidth, int frameHeight, int interval, bool isLoop)
        {
            this.gameObject = gameObject;
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            this.interval = interval;
            this.isLoop = isLoop;
        }
        
        public void Update(GameTime gameTime)
        {
            intervalAcc += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (intervalAcc > interval)
            {
                intervalAcc -= interval;
                actualWidth++;

                if (actualWidth * frameWidth >= gameObject.sprite.Width)
                {
                    actualHeight++;
                    actualWidth = 0;
                    if (actualHeight * frameHeight >= gameObject.sprite.Height)
                    {
                        completed = true;
                        if (isLoop)
                            actualHeight = 0;
                        else
                        {
                            actualHeight--;
                            actualWidth = (gameObject.sprite.Width / frameWidth) - 1;
                        }
                    }
                }
            }
            gameObject.sourceRectangle.X = actualWidth * frameWidth;
            gameObject.sourceRectangle.Y = actualHeight * frameHeight;
        }
    }
}